package Fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import Database.SQLquery;
import ListView_Adapter.PersonList_Adapter;
import ListView_Adapter.Person_Detail;
import ListView_Adapter.StudentList_Adapter;
import ListView_Adapter.Student_Detail;
import barcode.code.nhat.iuhbarcode.Main_Action;
import barcode.code.nhat.iuhbarcode.R;

public class Home_fragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    private static String id_class;

    public static Home_fragment newInstance(int sectionNumber,String id) {
        id_class = id;
        Home_fragment fragment = new Home_fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public Home_fragment(){

    }

    private SQLquery login;
    private ListView lv;
    private Button b1;
    private ArrayList<Person_Detail> mAdapter = new ArrayList<Person_Detail>();
    private PersonList_Adapter kid;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        login = new SQLquery(getActivity());
        login = login.open();

        String table = "check_" + id_class;
        String table0 = "CREATE TABLE IF NOT EXISTS " + table + "(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, id_student TEXT, date TEXT, periods INTEGER);";
        login.db.execSQL(table0);

        lv = (ListView) v.findViewById(R.id.list_id_scan);

        List<Person_Detail> per = login.get_list_person(table);
        for (Person_Detail cn : per) {
            mAdapter.add(cn);
        }
        kid = new PersonList_Adapter(getActivity(), mAdapter);
        lv.setAdapter(kid);

        b1 = (Button) v.findViewById(R.id.bt_update);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login.dropdown(id_class);
            }
        });

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((Main_Action) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
    }

}
